<template>
	<div class="add">
		<!--  -->
		<el-dialog v-model="isShowDialog" width="700px" :close-on-click-modal="false" :destroy-on-close="true">
			<template #header>
				<div>添加外呼线路</div>
			</template>
			<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
				<el-form-item label="线路编码" prop="code">
					<el-input v-model="formData.code" placeholder="请输入线路编码" />
				</el-form-item>
				<el-form-item label="线路名称" prop="name">
					<el-input v-model="formData.name" placeholder="请输入线路名称" />
				</el-form-item>
				<el-form-item label="描述" >
					<el-input v-model="formData.desc" type="textarea" placeholder="请输入描述" />
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit" :loading="loading">确 定</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent, ref, unref, getCurrentInstance } from 'vue';
import { ElMessageBox, ElMessage, FormInstance, UploadProps } from 'element-plus';
import { addOutcallLineApi } from '@/api/operationsManage/outboundLine'
export default defineComponent({
	name: 'outboundLineAdd', //外呼坐席账户管理 -新增
	setup(props, { emit }) {
		const formRef = ref<HTMLElement | null>(null);
		const menuRef = ref();
		const state = reactive({
			loading: false,
			isShowDialog: false,
			formData: {
				code:undefined,
				name:undefined,
				desc:undefined,
			},
			// 表单校验
			rules: {
				code: [{ required: true, message: '线路编码不能为空', trigger: 'blur' }],
				name: [{ required: true, message: '线路名称不能为空', trigger: 'blur' }],
			},
		});
		// 打开弹窗
		const openDialog = (row) => {
			resetForm();
			state.isShowDialog = true;
		};

		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};

		// 提交
		const onSubmit = () => {
			const formWrap = unref(formRef) as any;
			if (!formWrap) return;
			formWrap.validate((valid: boolean) => {
				if (valid) {
					addOutcallLineApi(state.formData).then((res: any) => {
						if (res.code === 200) {
							ElMessage.success('添加成功');
							closeDialog(); // 关闭弹窗
							emit('getList');
						} else {
							state.loading = false;
						}
					})
				}
			});
		};

		const resetForm = () => {
			state.formData = {
				code:undefined,
				name:undefined,
				desc:undefined,
			};
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			resetForm,
			menuRef,
			formRef,
			...toRefs(state),
		};
	},
});
</script>
